//
//  EditFoods.mm
//  Meal Wheel
//
//  Created by Justin Dell on 7/4/08.
//  Copyright 2008 . All rights reserved.
//

#import "EditFoods.h"
#import "FoodTree.h"
#include <string>
#include <vector>

NSMutableArray *foodlist;
FoodFetcher *fetcher;

@implementation EditFoods

-(void)initialize {
    if (!fetcher) fetcher = [[FoodFetcher alloc] init];
    foodlist = [fetcher getAllFoods];
    foodlist = [[foodlist sortedArrayUsingSelector:@selector(compare:)] retain];
}

-(IBAction)addFood:(id)sender {
    if ([[addFoodField stringValue] compare:@""] == NSOrderedSame) return;
    NSString *nsFood = [addFoodField stringValue];
    string food = [nsFood UTF8String];
    
    // Fill in the types depending on which buttons are pressed
    vector<string>types;
    if ([breadButton state]) types.push_back("Bread");
    if ([dairyButton state]) types.push_back("Dairy");
    if ([meatButton state]) types.push_back("Meat");
    if ([fruitButton state]) types.push_back("Fruit");
    if ([veggieButton state]) types.push_back("Veggie");
    if ([sweetButton state]) types.push_back("Sweet");
    
    // Add the food and save the file
    [fetcher addFood:food addType:types];
    [fetcher writeToFile];
    
    // Clear the data fields
    [addFoodField setStringValue:@""];
    [breadButton setState:0];
    [dairyButton setState:0];
    [meatButton setState:0];
    [fruitButton setState:0];
    [veggieButton setState:0];
    [sweetButton setState:0];
    [self initialize];
}

-(IBAction)removeFood:(id)sender {
    if ([[addFoodField stringValue] compare:@""] == NSOrderedSame) return;
    
    // Remove the food and save the file
    [fetcher removeFood:[[addFoodField stringValue] UTF8String]];
    [fetcher writeToFile];
    
    // Clear the data fields
    [addFoodField setStringValue:@""];
    [breadButton setState:0];
    [dairyButton setState:0];
    [meatButton setState:0];
    [fruitButton setState:0];
    [veggieButton setState:0];
    [sweetButton setState:0];
    [self initialize];
}

-(IBAction)editFood:(id)sender {
    if ([[addFoodField stringValue] compare:@""] == NSOrderedSame) return;
    
    // Modify the food by removing the current version and adding the new one
    [fetcher removeFood:[[addFoodField stringValue] UTF8String]];
    [self addFood:sender];
}

-(void)setStates:(NSString *) name {
    Food *food = [fetcher getFoodByName:[name UTF8String]]; 
    
    // Clear all states at first
    [breadButton setState:0];
    [dairyButton setState:0];
    [meatButton setState:0];
    [fruitButton setState:0];
    [veggieButton setState:0];
    [sweetButton setState:0];
    
    // Set the buttons depending on what states the food has
    vector<string> types = food->getTypes();
    for (int i = 0; i < types.size(); i++) {
        if (!types[i].compare("Bread")) [breadButton setState:1];
        if (!types[i].compare("Dairy")) [dairyButton setState:1];
        if (!types[i].compare("Meat"))  [meatButton setState:1];
        if (!types[i].compare("Fruit")) [fruitButton setState:1];
        if (!types[i].compare("Veggie"))[veggieButton setState:1];
        if (!types[i].compare("Sweet")) [sweetButton setState:1];
    }
}

- (int)numberOfItemsInComboBox:(NSComboBox *)aComboBox {
    return [foodlist count];
}

- (id)comboBox:(NSComboBox *)aComboBox objectValueForItemAtIndex:(int)index {
    return [foodlist objectAtIndex:index];
}

- (NSString *) firstGenreMatchingPrefix:(NSString *)prefix {
    NSString *theString = nil;
    NSString *lowercasePrefix = [prefix lowercaseString];
    NSEnumerator *stringEnum = [foodlist objectEnumerator];
    while ((theString = [stringEnum nextObject])) {
	if ([[theString lowercaseString] hasPrefix: lowercasePrefix]) return theString;
    }
    return nil;
}

- (NSString *)comboBox:(NSComboBox *)aComboBox completedString:(NSString *)inputString {
    // This method is received after each character typed by the user, because we have checked the "completes" flag for genreComboBox in IB.
    // Given the inputString the user has typed, see if we can find a genre with the prefix, and return it as the suggested complete string.
    NSString *candidate = [self firstGenreMatchingPrefix: inputString];
    if (candidate) {
        [self setStates:(candidate)];
        [foodLabel setStringValue:@"Edit Food"];
        [addButton setHidden:YES];
        [editButton setHidden:NO];
        [removeButton setEnabled:YES];
    }
    else {
        [breadButton setState:0];
        [dairyButton setState:0];
        [meatButton setState:0];
        [fruitButton setState:0];
        [veggieButton setState:0];
        [sweetButton setState:0];
        [foodLabel setStringValue:@"New Food"];
        [addButton setHidden:NO];
        [editButton setHidden:YES];
        [removeButton setEnabled:NO];
    }
    return (candidate ? candidate : inputString);
}

@end
